set(CXX_FLAGS
    -g
    -DCHECK_PTHREAD_RETURN_VALUE
    -D_FILE_OFFSET_BITS=64
    -Wall
    -Wextra
    -Werror
    -Wconversion
    -Wno-stringop-truncation
    -Wno-format-truncation
    -Wno-unused-parameter
    -Wno-unused-function
    -Wold-style-cast
    -Woverloaded-virtual
    -Wpointer-arith
    -Wwrite-strings
    -rdynamic
)
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")

MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM_NAME}")

if(${CMAKE_SYSTEM_NAME} STREQUAL "CYGWIN")
    string(APPEND CMAKE_CXX_FLAGS " -std=gnu++11 -D_GNU_SOURCE=1 ")
else()
    string(APPEND CMAKE_CXX_FLAGS " -std=c++11 ")
    MESSAGE(STATUS "add std c++11")
endif()
MESSAGE(STATUS "CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")

option(ENABLE_FLTO "Enable flto" OFF)

set(CMAKE_CXX_FLAGS_DEBUG "-O0")
if(${ENABLE_FLTO})
    MESSAGE(STATUS "${PROJECT_NAME} build type release, use lto optimize")
    set(CMAKE_CXX_FLAGS_RELEASE "-flto -O3 -DNDEBUG")
else()
    MESSAGE(STATUS "${PROJECT_NAME} build type release, not use lto optimize")
    set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
endif()

SET(CMAKE_BUILD_TYPE "$ENV{BUILD_TYPE}")
SET(CMAKE_USE_RELATIVE_PATHS "ON")
SET(CMAKE_CXX_COMPILER "g++")
SET(CMKAE_C_COMPILER "gcc")
SET(CMAKE_BUILD_TYPE Release)

if(${ENABLE_FLTO})
    message(STATUS "${PROJECT_NAME} build type release, use lto optimize")
    SET(CMAKE_AR "gcc-ar")
    SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
    SET(CMAKE_C_ARCHIVE_FINISH true)
    SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qcs <TARGET> <LINK_FLAGS> <OBJECTS>")
    SET(CMAKE_CXX_ARCHIVE_FINISH true)
endif()

option(AddVersion "add version info" OFF)

if(${AddVersion})
    ADD_DEFINITIONS(-DGITVISION_VER="$ENV{GIT_VER}")
    ADD_DEFINITIONS(-DCOMPILE_TIME="$ENV{CMP_TIME}")
else()
    message(STATUS "${PROJECT_NAME} not set version info")
endif()